package com.heiyanqiang.controller;

import com.github.pagehelper.PageInfo;
import com.heiyanqiang.domain.Orders;
import com.heiyanqiang.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    //未分页
    /*@RequestMapping("/findAll.do")
    public ModelAndView findAll() throws Exception{
        System.out.println("order is running ");
        ModelAndView mv = new ModelAndView();
        List<Orders> ordersList = ordersService.findAll();
        mv.addObject("ordersList",ordersList);
        mv.setViewName("orders-list");
        return mv;
    }*/
/*
@RequestParam(name = "page", required = true, defaultValue = "1")
Integer page, @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer
pageSize)
*
* */

    @RequestMapping("/findById.do")
    public ModelAndView findById( @RequestParam(name = "id",required = true) Integer ordersId) throws Exception{

        Orders orders = ordersService.findById(ordersId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("orders", orders);
        mv.setViewName("orders-show");
        return mv;
    }


    @RequestMapping("/findAll.do")
    public ModelAndView findAll(
            @RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", required = true, defaultValue = "5") Integer pageSize) throws Exception{

        ModelAndView mv = new ModelAndView();
        List<Orders> ordersList = ordersService.findAll(page,pageSize);
        PageInfo pageInfo = new PageInfo(ordersList);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("orders-page-list");
        return mv;
    }
}
